package Mobil;


import KonekDB.Koneksi;
import Mobil.Mobil;
import java.sql.*;
import java.sql.Connection;
import java.util.*;

/**
 *
 * @author Astri
 */
public class MobilKontrol {

    Connection konek;
    private static MobilKontrol mobilKontrol;
    private Mobil mobil;

    public MobilKontrol(Connection konek) {
        this.konek = konek;
    }

    public static MobilKontrol getKoneksiKontrol() {
        Koneksi k = new Koneksi();
        if (mobilKontrol == null) {
            mobilKontrol = new MobilKontrol(k.getConnection());
        }
        return mobilKontrol;
    }

    public void simpanMobil(Mobil mobil) throws SQLException {
        PreparedStatement prepare = null;
        String kode = mobil.getKode_Mobil();
        String tipe = mobil.getTipe_Mobil();
        String nopol = mobil.getNomor_Polisi();
        String wrn = mobil.getWarna();
        String stt = mobil.getStatus_Mobil();
        Double hs = mobil.getHarga_Sewa();
        
        String sql = "call tambahMobil(?,?,?,?,?,?)";
        prepare = konek.prepareStatement(sql);
        prepare.setString(1, kode);
        prepare.setString(2, tipe);
        prepare.setString(3, nopol);
        prepare.setString(4, wrn);
        prepare.setString(5, stt);
        prepare.setDouble(6, hs);
        prepare.executeUpdate();

    }
    
    public void ubahMobil(Mobil mobil) throws SQLException {
        PreparedStatement prepare = null;
        String kode = mobil.getKode_Mobil();
        String tipe = mobil.getTipe_Mobil();
        String nopol = mobil.getNomor_Polisi();
        String wrn = mobil.getWarna();
        String stt = mobil.getStatus_Mobil();
        Double hs = mobil.getHarga_Sewa();
        String sql = "call updateMobil(?,?,?,?,?,?)";
        prepare = konek.prepareStatement(sql);
        prepare.setString(1, kode);
        prepare.setString(2, tipe);
        prepare.setString(3, nopol);
        prepare.setString(4, wrn);
        prepare.setString(5, stt);
        prepare.setDouble(6, hs);
        prepare.executeUpdate();
    }
    
    public void hapusMobil(Mobil mobil) throws SQLException {
        PreparedStatement prepare = null;
        String kode = mobil.getKode_Mobil();
        String sql = "call hapusMobil(?)";
        prepare = konek.prepareStatement(sql);
        prepare.setString(1, kode);
        prepare.executeUpdate();
    }

    public List<Mobil> getAllMobil() throws SQLException {
        PreparedStatement prepare = null;
        ResultSet rs = null;


        String sql = "select Kode_Mobil,Tipe_Mobil,Nomor_Polisi,Warna,Status_Mobil,Harga_Sewa from mobil order by Kode_Mobil";
        prepare = konek.prepareStatement(sql);
        rs = prepare.executeQuery();
        List<Mobil> mobil = new ArrayList<Mobil>();
        while (rs.next()) {
            Mobil mb = new Mobil();
            String kode = rs.getString("Kode_Mobil");
            String tipe = rs.getString("Tipe_Mobil");
            String nopol = rs.getString("Nomor_Polisi");
            String wrn = rs.getString("Warna");
            String stt = rs.getString("Status_Mobil");
            Double hs = rs.getDouble("Harga_Sewa");
            
            mb.setKode_Mobil(kode);
            mb.setTipe_Mobil(tipe);
            mb.setNomor_Polisi(nopol);
            mb.setWarna(wrn);
            mb.setStatus_Mobil(stt);
            mb.setHarga_Sewa(hs);
            mobil.add(mb);
        }
        konek.commit();
        return mobil;
    }    
    
    public Mobil cariMobil(Mobil mobil) throws SQLException{
        CallableStatement callst = null;
        
        String sql = "call cariMobil(?,?,?,?,?,?)";
        callst = konek.prepareCall(sql);
        callst.setString(1, mobil.getKode_Mobil());
        callst.registerOutParameter(2, java.sql.Types.VARCHAR);
        callst.registerOutParameter(3, java.sql.Types.VARCHAR);
        callst.registerOutParameter(4, java.sql.Types.VARCHAR);
        callst.registerOutParameter(5, java.sql.Types.VARCHAR);
        callst.registerOutParameter(6, java.sql.Types.DOUBLE);
        callst.execute();
        
        String tipe = callst.getString(2);
        String nopol = callst.getString(3);
        String warna = callst.getString(4);
        String status = callst.getString(5);
        Double hs = callst.getDouble(6);
        callst.execute();
        
        mobil.setTipe_Mobil(tipe);
        mobil.setNomor_Polisi(nopol);
        mobil.setWarna(warna);
        mobil.setStatus_Mobil(status);
        mobil.setHarga_Sewa(hs);
        return mobil;       
    }
}
